class SpriteManager {
    constructor(app) {
        this.app = app;
        this.sprites = new Map();
        this.loadOrder = [];
    }

    // 注册精灵类
    register(name, SpriteClass) {
        if (this.sprites.has(name)) {
            console.warn(`Sprite ${name} already registered`);
            return;
        }
        
        const sprite = new SpriteClass(this.app);
        this.sprites.set(name, sprite);
        this.loadOrder.push(name);
    }

    // 批量注册精灵
    registerBatch(spriteConfigs) {
        spriteConfigs.forEach(({ name, sprite }) => {
            this.register(name, sprite);
        });
    }

    // 获取精灵实例
    get(name) {
        return this.sprites.get(name);
    }

    // 检查精灵是否存在
    has(name) {
        return this.sprites.has(name);
    }

    // 移除精灵
    remove(name) {
        const sprite = this.sprites.get(name);
        if (sprite) {
            sprite.cleanup();
            this.sprites.delete(name);
            
            const index = this.loadOrder.indexOf(name);
            if (index > -1) {
                this.loadOrder.splice(index, 1);
            }
        }
    }

    // 批量初始化所有精灵
    async initAll() {
        for (const name of this.loadOrder) {
            const sprite = this.sprites.get(name);
            if (sprite && !sprite.isLoaded()) {
                console.log(`Initializing sprite: ${name}`);
                await sprite.setup();
            }
        }
    }

    // 更新所有激活的精灵
    updateAll(deltaTime) {
        this.sprites.forEach(sprite => {
            if (sprite.isActive() && sprite.isLoaded()) {
                sprite.update(deltaTime);
            }
        });
    }

    // 激活/停用精灵
    setActive(name, active) {
        const sprite = this.sprites.get(name);
        if (sprite) {
            sprite.setActive(active);
        }
    }

    // 激活/停用所有精灵
    setAllActive(active) {
        this.sprites.forEach(sprite => {
            sprite.setActive(active);
        });
    }

    // 获取所有精灵名称
    getNames() {
        return Array.from(this.sprites.keys());
    }

    // 获取所有已加载的精灵
    getLoadedSprites() {
        const loaded = [];
        this.sprites.forEach((sprite, name) => {
            if (sprite.isLoaded()) {
                loaded.push({ name, sprite });
            }
        });
        return loaded;
    }

    // 获取所有激活的精灵
    getActiveSprites() {
        const active = [];
        this.sprites.forEach((sprite, name) => {
            if (sprite.isActive()) {
                active.push({ name, sprite });
            }
        });
        return active;
    }

    // 清理所有精灵
    destroy() {
        this.sprites.forEach(sprite => {
            sprite.cleanup();
        });
        this.sprites.clear();
        this.loadOrder = [];
    }

    // 获取统计信息
    getStats() {
        let loaded = 0;
        let active = 0;
        
        this.sprites.forEach(sprite => {
            if (sprite.isLoaded()) loaded++;
            if (sprite.isActive()) active++;
        });

        return {
            total: this.sprites.size,
            loaded,
            active
        };
    }
}

export default SpriteManager;
